 @include "lib.awk"
 function binsort(bins0,bins,sort,    bins1,s,sq,b,x,y,l) {
   for(b in bins0) {
     
     l = length(bins0[b])
     bins1[b]["key"] = b
     s=sq=0
     for(x in bins0[b]){ y= bins0[b][x]; s += y; sq += y*y }
     bins1[b]["s"]   = s
     bins1[b]["sq"]  = sq
     bins1[b]["mean"]= s/l
     bins1[b]["n"]   = l
   }
   return asort(bins1, bins,sort ? sort : "meanAsc")
 }
 function meanAsc(a,x,b,y) { 
   return x["mean"]  - y["mean"] 
 }
 function sds(min,max,bins,    s,sq,n,b) {
   for(b = min; b<=max;b++) {
     n  += bins[b]["n"]
     s  += bins[b]["s"]
     sq += bins[b]["sq"]
   }
   return sd(n,s,sq)
 }
 function ns(min,max,bins,   b,n) {
   for(b=min;b<=max;b++) 
     n += bins[b]["n"]
   return n
 }
 function means(min,max,bins,   b,sum,n) {
   for(b=min;b<=max;b++) { 
     sum += bins[b]["s"]
     n   += bins[b]["n"]
   }
   return sum / n
 }
